---------------prepare runenv----------------
local my_path = "/home/lius/workspace/conf_itf/"
local m_package_path = package.path  
package.path = string.format("%s;%s?.lua",  
    m_package_path, my_path)
    
local my_cpath = "/home/lius/workspace/load_pcap/"
local m_package_cpath = package.cpath
package.cpath = string.format("%s;%s?.so",  
    m_package_cpath, my_cpath)
--print(package.path,package.cpath)

---------------load base module----------------
require "base_itf"
require "base_proto"
require("libload_pcap")

---------------load proto module---------------
local proto = nil
if pcall(require, base_itf.conf_box.proto) then
	proto = require(base_itf.conf_box.proto)
else
	proto = nil
end

---------------module def----------------------
--module ("base_wrap" , package.seeall)

---------------Debug switch--------------------
--Debug = 1
Debug = nil
--------------------------pcap process-------------------------------
--[[
wrap result table style:
{
	{
		{cli={{0,""}},ser={{0,""},{1,"a"}}},
		{cli={{0,""}},ser={{0,""},{1,"a"}}},
		{cli={{0,""}},ser={{0,""},{1,"a"}}},
		{cli={{0,""}},ser={{0,""},{1,"a"}}},
	},
}
--]]
local function __load_pcap(pcap_file, filter)
	final_res = {}
	result = {}
	sub_result = {cli={},ser={}}
	print("pcap_init result is : ", load_pcap.pcap_init(pcap_file, filter))
	tcp_stat = 0
	last_side = 0

	while tcp_stat ~=-1 do
		tcp_stat,tcp_side,tcp_data_len,tcp_data = load_pcap.get_next()
		if tcp_stat == 1 then
			--conn est
			result = {}
		elseif tcp_stat == 2 then
			--conn data arrive
			if tcp_side ~= last_side then
				if tcp_side == 0 then
					-- client side
					table.insert(result, sub_result)
					sub_result = {cli={},ser={}}
					table.insert(sub_result.cli, {tcp_data_len, tcp_data});
				else
					-- server side
					table.insert(sub_result.ser, {tcp_data_len, tcp_data});
				end
				last_side = tcp_side
				--print("--------------------------------")
				--print(tcp_stat, tcp_side,tcp_data_len,tcp_data)
			else
				if tcp_side == 0 then
					-- client side
					table.insert(sub_result.cli, {tcp_data_len, tcp_data});
				else
					-- server side
					table.insert(sub_result.ser, {tcp_data_len, tcp_data});
				end
				--print(tcp_data)
			end
		elseif tcp_stat == 3 or tcp_stat == 4 then
			--conn closed
			table.insert(result, sub_result)
			table.insert(final_res, result)
		end
		
	end
	
	load_pcap.pcap_exit()
	
	print("link numbers:", table.getn(final_res))
	return final_res
end

------------------------config process------------------------

local function get_res(cmd, cmd_conf, line_split, cmd_split)
	res = base_proto.get_res(cmd, cmd_conf, line_split, cmd_split)
	return {string.len(res), res}
end

local function __load_proto(proto)
	local cmd_seq = proto.cmd_seq
	local cmd_conf = proto.cmd_conf
	local cmd_split = proto.cmd_split
	local line_split = proto.line_split
	
	local final_res = {}
	local result = {}
	local sub_result = {cli={nil,nil},ser={nil,nil}}
	
	conn_iter = base_proto.get_cmd_iter(cmd_seq, cmd_conf)

	cmd = base_proto.get_cmd(conn_iter)


	if cmd == "banner" then
		table.insert(result, {cli={nil}, ser = {get_res(cmd, cmd_conf, line_split, cmd_split)}})
	else
		table.insert(result, {cli={{string.len(cmd), cmd}}, ser = {get_res(cmd, cmd_conf, line_split, cmd_split)}})
	end
	cmd = base_proto.get_cmd(conn_iter)
	while cmd ~= nil do
		table.insert(result, {cli={{string.len(cmd), cmd}}, ser = {get_res(cmd, cmd_conf, line_split, cmd_split)}})
		cmd = base_proto.get_cmd(conn_iter)
	end
	table.insert(final_res, result)
	return final_res
end

function __parse_ra_tbl(tbl)
	for l,x in ipairs(tbl) do
		for j,v in ipairs(x) do
			for k,y in ipairs(v.cli) do
				print("client:",y[1], y[2])
			end
			for k,y in ipairs(v.ser) do
				print("server:",y[1], y[2])
			end		
		end
	end
end

------------------------wrap process------------------------

function load_conf()
	local conf = base_itf.conf_box
	if proto then
		return __load_proto(proto)
	elseif conf.pcap.file then
		return __load_pcap(conf.pcap.file, conf.pcap.filter)
	else
		print("ERROR: conf_box error!")
		return nil
	end
end

----------------------test --------------------------------------
if Debug then
	ra_tbl = load_conf()
	__parse_ra_tbl(ra_tbl)
end
